<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="语言" prop="language">
        <el-input
          v-model="queryParams.language"
          placeholder="请输入语言"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国家" prop="country">
        <el-input
          v-model="queryParams.country"
          placeholder="请输入国家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布时间" prop="releaseTime">
        <el-date-picker
          clearable
          v-model="queryParams.releaseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table
      v-loading="loading"
      :data="articleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="aId" /> -->
      <el-table-column label="语言" align="center" prop="language" />
      <el-table-column label="标题" align="center" prop="title" />
      <!-- <el-table-column label="所属标签id" align="center" prop="label" /> -->
      <el-table-column label="时间" align="center" prop="time" />
      <el-table-column label="来源" align="center" prop="source" />
      <!-- <el-table-column label="内容" align="center" prop="content" /> -->
      <el-table-column label="简介" align="center" prop="brief" />
      <el-table-column
        label="封面图"
        align="center"
        prop="coverImage"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="大图" align="center" prop="bigImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.bigImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">news</span>
          <span v-if="scope.row.type == 2">events</span>
        </template>
      </el-table-column>
      <el-table-column label="国家" align="center" prop="country" />
      <el-table-column label="地点" align="center" prop="location" />
      <!-- <el-table-column
        label="是否有活动需要网上报名"
        align="center"
        prop="isActivity"
      />
      <el-table-column
        label="活动所需要的信息"
        align="center"
        prop="activityInformation"
      />
      <el-table-column
        label="事件描述"
        align="center"
        prop="enentDescription"
      />
      <el-table-column label="默认0；删除1" align="center" prop="isDelete" />
      <el-table-column
        label="发布时间"
        align="center"
        prop="releaseTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="${comment}" align="center" prop="userId" />
      <el-table-column label="发布人" align="center" prop="releaseBy" />
      <el-table-column
        label="是否发布默认1，0发布"
        align="center"
        prop="isRelease"
      /> -->
      <!-- <el-table-column
        label="是否置顶，默认0,1置顶，2置顶和列表"
        align="center"
        prop="top"
      />
      <el-table-column
        label="是否首页，默认0,1首页"
        align="center"
        prop="isHome"
      />
      <el-table-column
        label="是否轮播，默认0,1首页"
        align="center"
        prop="isBanner"
      />
      <el-table-column
        label="审核状态，默认1未审核，2提交审核,3审核完成"
        align="center"
        prop="audit"
      />
      <el-table-column
        label="多媒体，1视频，2图片"
        align="center"
        prop="multimedia"
      />
      <el-table-column label="排序" align="center" prop="num" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:article:edit']"
            >修改</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePreview(scope.row)"
            v-hasPermi="[]"
            >预览</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSeeActive(scope.row)"
            v-hasPermi="[]"
            v-if="scope.row.isActivity == 'yes'"
            >查看活动</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSubAdmin(scope.row)"
            v-hasPermi="[]"
            >审核</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:article:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="提示"
      :visible.sync="subAdminFlag"
      width="30%"
      :before-close="handleClose"
    >
      <span>是否审核通过</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="noPassFn">驳回</el-button>
        <el-button type="primary" @click="passFn">通过</el-button>
      </span>
    </el-dialog>
    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="语言" prop="language">
          <el-input v-model="form.language" placeholder="请输入语言" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="form.title"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="所属标签id" prop="label">
          <el-input v-model="form.label" placeholder="请输入所属标签id" />
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-input v-model="form.time" placeholder="请输入时间" />
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="简介" prop="brief">
          <el-input
            v-model="form.brief"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="封面图" prop="coverImage">
          <image-upload v-model="form.coverImage" />
        </el-form-item>
        <el-form-item label="大图" prop="bigImage">
          <image-upload v-model="form.bigImage" />
        </el-form-item>
        <el-form-item label="国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入国家" />
        </el-form-item>
        <el-form-item label="地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入地点" />
        </el-form-item>
        <el-form-item label="是否有活动需要网上报名" prop="isActivity">
          <el-input
            v-model="form.isActivity"
            placeholder="请输入是否有活动需要网上报名"
          />
        </el-form-item>
        <el-form-item label="活动所需要的信息" prop="activityInformation">
          <el-input
            v-model="form.activityInformation"
            placeholder="请输入活动所需要的信息"
          />
        </el-form-item>
        <el-form-item label="事件描述" prop="enentDescription">
          <el-input
            v-model="form.enentDescription"
            placeholder="请输入事件描述"
          />
        </el-form-item>
        <el-form-item label="默认0；删除1" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入默认0；删除1" />
        </el-form-item>
        <el-form-item label="发布时间" prop="releaseTime">
          <el-date-picker
            clearable
            v-model="form.releaseTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="发布人" prop="releaseBy">
          <el-input v-model="form.releaseBy" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="是否发布默认1，0发布" prop="isRelease">
          <el-input
            v-model="form.isRelease"
            placeholder="请输入是否发布默认1，0发布"
          />
        </el-form-item>
        <el-form-item label="是否置顶，默认0,1置顶，2置顶和列表" prop="top">
          <el-input
            v-model="form.top"
            placeholder="请输入是否置顶，默认0,1置顶，2置顶和列表"
          />
        </el-form-item>
        <el-form-item label="是否首页，默认0,1首页" prop="isHome">
          <el-input
            v-model="form.isHome"
            placeholder="请输入是否首页，默认0,1首页"
          />
        </el-form-item>
        <el-form-item label="是否轮播，默认0,1首页" prop="isBanner">
          <el-input
            v-model="form.isBanner"
            placeholder="请输入是否轮播，默认0,1首页"
          />
        </el-form-item>
        <el-form-item
          label="审核状态，默认1未审核，2提交审核,3审核完成"
          prop="audit"
        >
          <el-input
            v-model="form.audit"
            placeholder="请输入审核状态，默认1未审核，2提交审核,3审核完成"
          />
        </el-form-item>
        <el-form-item label="多媒体，1视频，2图片" prop="multimedia">
          <el-input
            v-model="form.multimedia"
            placeholder="请输入多媒体，1视频，2图片"
          />
        </el-form-item>
        <el-form-item label="排序" prop="num">
          <el-input v-model="form.num" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listArticle,
  getArticle,
  delArticle,
  addArticle,
  updateArticle,
  passAdminAudit,
  noPassAdminAudit,
} from "@/api/system/audit";

export default {
  name: "Article",
  data() {
    return {
      subAdminFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        language: null,
        title: null,
        label: null,
        time: null,
        source: null,
        content: null,
        brief: null,
        coverImage: null,
        bigImage: null,
        type: null,
        country: null,
        location: null,
        isActivity: null,
        activityInformation: null,
        enentDescription: null,
        isDelete: null,
        releaseTime: null,
        userId: null,
        releaseBy: null,
        isRelease: null,
        top: null,
        isHome: null,
        isBanner: null,
        audit: null,
        multimedia: null,
        num: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      currentObj: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    passFn() {
      passAdminAudit({ aId: this.currentObj.aId }).then((res) => {
        if (res.code == 200) {
          this.subAdminFlag = false;
          this.$message.success("审核通过！");
        }
      });
    },
    noPassFn() {
      noPassAdminAudit({ aId: this.currentObj.aId }).then((res) => {
        if (res.code == 200) {
          this.subAdminFlag = false;
          this.$message.error("审核未通过！");
        }
      });
    },
    handleClose(done) {
      this.subAdminFlag = false;
    },
    //预览
    handlePreview(row) {
      if (row.type == 1) {
        window.open(
          `https://wfuca.org/articleDetail2?id=${row.aId}&lang=EN&token=eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjVmMjNjMzg3LTQzZjAtNGFlYi1hM2Y0LTIyYzhjYTUyNWU4YyJ9.KXcbb9acl_qjdSFpuqwsc1BXxwoBVXjEBus1BbV2BptbQ-mFj98W62cMyqNIVOk_TcNC25cy_IDzjvxAYZS6iQ`,
          "_blank"
        );
      } else {
        window.open(
          `https://wfuca.org/articleDetail?id=${row.aId}&lang=EN&token=eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjVmMjNjMzg3LTQzZjAtNGFlYi1hM2Y0LTIyYzhjYTUyNWU4YyJ9.KXcbb9acl_qjdSFpuqwsc1BXxwoBVXjEBus1BbV2BptbQ-mFj98W62cMyqNIVOk_TcNC25cy_IDzjvxAYZS6iQ`,
          "_blank"
        );
      }
    },
    //查看活动
    handleSeeActive(row) {},
    //审核
    handleSubAdmin(row) {
      this.subAdminFlag = true;
      this.currentObj = row;
    },
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then((response) => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        aId: null,
        language: null,
        title: null,
        label: null,
        time: null,
        source: null,
        content: null,
        brief: null,
        coverImage: null,
        bigImage: null,
        type: null,
        country: null,
        location: null,
        isActivity: null,
        activityInformation: null,
        enentDescription: null,
        isDelete: null,
        createTime: null,
        updateTime: null,
        releaseTime: null,
        createBy: null,
        userId: null,
        releaseBy: null,
        isRelease: null,
        top: null,
        isHome: null,
        isBanner: null,
        audit: null,
        multimedia: null,
        num: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.aId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const aId = row.aId || this.ids;
      getArticle(aId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.aId != null) {
            updateArticle(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const aIds = row.aId || this.ids;
      this.$modal
        .confirm('是否确认删除文章编号为"' + aIds + '"的数据项？')
        .then(function () {
          return delArticle(aIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/article/export",
        {
          ...this.queryParams,
        },
        `article_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
